package com.example.treehole.ui.page.login

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.treehole.ui.component.showLoginSnackbar
import com.example.treehole.ui.model.InputCardType
import com.example.treehole.ui.theme.Typography
import com.example.treehole.ui.theme.mediumDarkCyan
import kotlinx.coroutines.launch

@Composable
fun RegisterScreen(
    onChangeScreen: () -> Unit,
    isChecked:Boolean
) {
    val loginViewModel = LocalLoginViewModel.current
    val inputEmail by loginViewModel.inputEmail.collectAsState()
    val scope = rememberCoroutineScope()

    var inputPassword by remember { mutableStateOf("") }
    var inputPasswordAgain by remember { mutableStateOf("") }
    var inputVerifyCode by remember { mutableStateOf("") }

    var errorMessage1 by remember { mutableStateOf("") }
    var errorMessage2 by remember { mutableStateOf("") }
    var errorMessage3 by remember { mutableStateOf("") }
    var errorMessage4 by remember { mutableStateOf("") }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
            .background(Color.Transparent),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(8.dp)
                .weight(5f),
            verticalArrangement = Arrangement.Bottom,
            horizontalAlignment = Alignment.Start
        ) {
            Text(text = "注册", style = Typography.titleLarge)
            Spacer(modifier = Modifier.height(8.dp))
            Text(text = "成为树洞的新朋友", style = Typography.titleLarge)
        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .weight(11f)
                .padding(vertical = 17.dp),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            InputCard(
                inputValue = inputEmail,
                inputCardType = InputCardType.EMAIL,
                onInputValueChange = { loginViewModel.updateInputEmail(it) },
                onErrorMessageChange = { errorMessage1 = it }
            )
            WarningCard(errorMessage1)
            InputCard(
                inputValue = inputPassword,
                inputCardType = InputCardType.PASSWORD,
                onInputValueChange = { inputPassword = it },
                onErrorMessageChange = { errorMessage2 = it }
            )
            WarningCard(errorMessage2)
            InputCard(
                inputValue = inputPasswordAgain,
                inputCardType = InputCardType.PASSWORD_AGAIN,
                passWord = inputPassword,
                onInputValueChange = { inputPasswordAgain = it },
                onErrorMessageChange = { errorMessage3 = it }
            )
            WarningCard(errorMessage3)
            InputCard(
                inputValue = inputVerifyCode,
                inputCardType = InputCardType.VERIFY_CODE,
                onInputValueChange = { inputVerifyCode = it },
                onErrorMessageChange = { errorMessage4 = it }
            )
            WarningCard(errorMessage4)
            Column(
                modifier = Modifier.padding(24.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Button(
                    onClick = {
                        scope.launch {
                            if(isChecked) {
                                if (inputPassword == inputPasswordAgain) {
                                    if (loginViewModel.registerUser(
                                            inputPassword,
                                            inputVerifyCode
                                        )
                                    ) {
                                        onChangeScreen()
                                    }
                                } else {
                                    showLoginSnackbar("两次密码输入不相同")
                                }
                            } else{
                                showLoginSnackbar("请同意《用户协议》和《隐私政策》")
                            }
                        }
                    },
                    modifier = Modifier
                        .size(150.dp, 60.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = mediumDarkCyan,
                        contentColor = Color.DarkGray
                    ),
                    shape = RoundedCornerShape(16.dp)
                ) {
                    Text(text = "注册", fontSize = 16.sp)
                }
                TextButton(
                    onClick = onChangeScreen,
                    colors = ButtonDefaults.textButtonColors(
                        contentColor = Color.DarkGray
                    )
                ) {
                    Text("已有账号？去登录一下吧")
                }

            }
        }

    }


}